//系统配置-操作管理-批量添加组件
App.util.ns('Admin.Access.Operation');
Admin.Access.Operation.MultiAdd = function(dataTable){
    var win = new App.Win({name: 'multi_add'});
    //列表
    var form = new App.Form({
        items: [
            {type: 'select', name: 'group_id', title: '所属分组', items: App.models.get('operationGroups'), item_key: 'group_id', item_name: 'name'},
            {type: 'textarea', name: 'items', title: '路由片段', 
                rule: {
                    type: 'str',
                    range: [1, 1000]
                }
            }
        ]
    });
    form.formItems['items'].frame.css({height: '300px'});
    form.formItems['items'].frame.find('.form-control').css({height: '100%'});
    win.form = form;
    win.setContent(form.frame);
    //设置按钮
    win.setBtns([(function(){
        var cancelBtn = $('<a class="btn btn-danger btn-xs m-right-sm">取消</a>');
        cancelBtn.click(function(){
            win.hide();
        });
        return cancelBtn;
    })(), (function(){
        var saveBtn = $('<a class="btn btn-success btn-xs">保存</a>');
        saveBtn.click(function(){
            var groupId = win.form.formItems['group_id'].value();
            var itemsStr = win.form.formItems['items'].value();
            if(groupId.length < 1 || itemsStr.length < 1){
                App.util.hint.toast({
                    type: 'error',
                    text: '格式不正确'
                });
                return;
            }
            var itemsRaw = itemsStr.split("\n");
            var items = [];
            var namespace = 'Admin';
            for(var i in itemsRaw){
                var itemStr = itemsRaw[i];
                //匹配命名空间
                if(itemStr.indexOf('Route::group') > -1){
                    var namespaceMatch = itemStr.match(/.*?\'namespace\'.*?\'(.*?)\'/);
                    if(namespaceMatch){
                        namespace += '\\'+namespaceMatch[1];
                    }
                    continue;
                }
                var itemMatch = itemStr.match(/.*?::(.*?)\(.*,.*['"](.*?)['"].*?\/\/(.*)/);
                if(itemMatch && itemMatch.length === 4){
                    var type = itemMatch[1];
                    var code = itemMatch[2];
                    var name = itemMatch[3];
                    if(code.length < 1 || name.length < 1){
                        App.util.hint.toast({
                            type: 'error',
                            text: '路由格式有误1'
                        });
                        return ;
                    }
                    var item = {
                        code: namespace ? namespace+'\\'+code : code,
                        name: name,
                        data_source: '',
                        data_id: 'id',
                        data_name: 'name'
                    };
                    if(type === 'get'){
                        items.push(item);
                        continue;
                    }
                    var method = code.split('@')[1];
                    if(method === 'create'){
                        item['data_source'] = '3';
                    }else if(method === 'update'){
                        item['data_source'] = '4';
                    }else if(method === 'delete'){
                        item['data_source'] = '6';
                    }else if(method === 'multiDelete'){
                        item['data_source'] = '6';
                    }else if(method === 'sort'){
                        item['data_source'] = '1';
                    }else{
                        item['data_source'] = '5';
                    }
                    items.push(item);
                }
            }
            if(items.length < 1){
                App.util.hint.toast({
                    type: 'error',
                    text: '未匹配到'
                });
                return ;
            }
            dataTable.postData({
                url: 'multiAdd',
                alert: '保存成功！',
                reload: true,
                data: {
                    group_id: groupId,
                    items: JSON.stringify(items)
                },
                after: function(){
                    win.hide();
                }
            });
        });
        return saveBtn;
    })()]);
    win.hide();
    return win;
};